package com.maimao.user.service;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.extra.mail.MailException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.maimao.core.constant.ListData;
import com.maimao.core.exception.MaiMaoDaoException;
import com.maimao.core.model.resp.PageResult;
import com.maimao.core.utils.DateUtils;
import com.maimao.model.user.Enterprise;
import com.maimao.model.user.Shop;
import com.maimao.model.user.User;
import com.maimao.model.user.enums.RoleEnum;
import com.maimao.model.user.enums.ShopStatus;
import com.maimao.user.mapper.ShopMapper;
import com.maimao.user.mapper.UserMapper;
import com.maimao.user.mapper.sys.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.io.Serializable;
import java.util.List;

import static com.maimao.core.constant.Constants.ONE;
import static com.maimao.core.constant.Constants.TWO;

/**
 * @author MaoLin Wang
 * @date 2020/12/22 7:44 下午
 */
@Service
public class ShopService {
    public Shop findShopByOwner(Long owner, Integer social) {
        return shopMapper.findShopByOwnerAndSocial(owner, social);
    }

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;


    public static final Integer ENTERPRISE = 1;
    public static final Integer NORMAL = 2;

    public Shop queryById(Long id) {
        return shopMapper.selectById(id);
    }

    /**
     * 根据店铺id查询用户邮箱
     *
     * @param id 店铺id
     * @return
     */
    public String queryUserEmailById(Long id) {
        return shopMapper.queryUserEmailIdById(id);
    }

    public PageResult<? extends Serializable> queryToCheck(Integer shopType, Integer page, Integer size) {
        if (shopType.equals(NORMAL)) {
            //普通店铺
            PageHelper.startPage(page, size);
            List<User> users = userService.queryShopCheck();
            return new PageResult<>(users);
        } else {
            PageHelper.startPage(page, size);
            List<Enterprise> enterpriseList = companyService.queryShopCheck();
            return new PageResult<>(enterpriseList);
        }
    }

    /**
     * 审核店铺
     *
     * @param status 1通过 2 不通过
     * @param userId 用户id
     */
    @Transactional(rollbackFor = MaiMaoDaoException.class)
    public void check(Integer status, Long userId, Integer shopType) {
        if (shopType.equals(TWO)) {
            if (status.equals(ONE)) {
                //通过
                userMapper.updateShopStatus(userId, ShopStatus.OPEN.getCode());
                User user = userMapper.selectById(userId);
                if (user == null) {
                    throw new MailException("用户不存在");
                }
                shopMapper.insert(Shop.builder()
                        .createTime(DateUtils.now())
                        .name(user.getUsername())
                        .owner(userId)
                        .social(0)
                        .build());
                sysUserMapper.addUserRoleRecord(userId, Lists.newArrayList(RoleEnum.ROLE_WEB_SHOP.getId()));

            } else {
                userMapper.updateShopStatus(userId, ShopStatus.FAIL.getCode());
            }
        }

    }

    /**
     * 修改店铺绑定的品牌
     */
    @Transactional(rollbackFor = MaiMaoDaoException.class)
    public void bindBrand(Long brandId, Long shopId) {
        shopMapper.updateBrandId(shopId, brandId);
    }
}
